package com.kt.controller;

import com.alibaba.fastjson.JSON;
import com.kt.domain.User;
import com.kt.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/changeUserInfo.do")
public class ChangeUserInfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");

        req.setCharacterEncoding("utf-8");

        UserServiceImp userServiceImp = new UserServiceImp();
        User user = userServiceImp.findByUid(req.getParameter("uid"));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        // 手机号暂时无法修改
//        user.setPhone(req.getParameter("phone"));
        user.setName(req.getParameter("name"));
        user.setSex(req.getParameter("sex"));
        int result = userServiceImp.changeUserInfo(user);
        if (result == 0) {
            resp.getWriter().write("0");
            return;
        }
        resp.getWriter().write(JSON.toJSONString(user));
    }
}
